package com.dgt.app.OCRDGT.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;



public class UtilsOCR {

	 private static Connection conn;

	private static Properties propPaths;

	private static Logger logger= Logger.getLogger(UtilsOCR.class.getName());

	public static void loadProperties(){
         propPaths = new Properties();
         InputStream is = null;
 		try {
 			is=new FileInputStream(new File("./paths.properties"));
 			propPaths.load(is);
 			is.close();
 		}catch(IOException ioe) {
 			logger.error(ioe.getMessage());
 			ioe.printStackTrace();
 		}
	 }
	 
	 public static Properties getPropertiesPaths(){
		 if(propPaths==null){
			 loadProperties();
		 }
		 return propPaths;
	 }
	 
	 
	 /**
		 * Move sourceFile to destinationFile
		 * @param sourceFile
		 * @param destinationFile
		 */
		 public static boolean fileMove(String sourceFile, String destinationFile) {
			 boolean moved=false;
			 //logger.info("Mover fichero Desde: " + sourceFile);
			 //logger.info("Hacia: " + destinationFile);

			 File source = new File(sourceFile);
			 File outFile = new File(destinationFile);
			 if(source.exists()) {
			             try {
							FileUtils.moveFile(source, outFile);
							 logger.info("Movido fichero Desde: " + sourceFile);
							 logger.info("Hacia: " + destinationFile);
							 moved=true;
						} catch (IOException e) {
							 logger.error("No ha sido posible mover "+sourceFile);
						}
			 }

			return moved;	
		}
		 
		public static Connection connectionOracleGestDocsBBDD() throws SQLException {
		//  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			if(conn==null){
				String ipServidorBBDD=UtilsOCR.getPropertiesPaths().getProperty("IPSERVIDORBBDD");
				String puerto=UtilsOCR.getPropertiesPaths().getProperty("PUERTO");
				String usuario=UtilsOCR.getPropertiesPaths().getProperty("USUARIO");
				String password=UtilsOCR.getPropertiesPaths().getProperty("PASSWORD");
				String esquemaBBDD=UtilsOCR.getPropertiesPaths().getProperty("ESQUEMABBDD");
				DriverManager.setLoginTimeout(0); 
				conn = DriverManager.getConnection("jdbc:oracle:thin:"+ipServidorBBDD+":"+puerto+":"+esquemaBBDD, usuario, password);
				Statement s=conn.createStatement();    
				s.execute("select 1 from dual");    
				s.close();
			}
			else if(conn.isClosed()){
				conn=null;
				conn=connectionOracleGestDocsBBDD();
			}
		    return conn;
		}
		public static void closeConnection() {
//			try {
//				conn.close();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
		}

}

